/**

 * @(#)Condition.java       2008-8-17
 */
package edu.pku.ss.mumcut.common;

/**This class is used to represent as a condition, a condition have two element
 * First the literal that represent the condition, the other is that is negative or not
 * For example, a boolean expression "ab+c!d", 'a', 'b', 'c' and 'd' are literals 
 * in the other side 'a' is a condition that not negative, so as 'b' and 'c',
 * '!d' is a condition that is negative
 * of course there may have '!!a', it is equals to 'a'
 * 
 * @author power
 *
 */
public class Condition implements Cloneable{
	/**
	 * the literal of the Condition
	 *
	 */
	private String literal;
	
	/**The 
	 * if the literal has a not operator, this will be false
	 *
	 */
	private boolean positive;
	
	
	@Override
	//Be cause that after clone(), the positive may change,so 
	//In order to keep the independence of the condition,
	//we have not use same reference with the original one
	protected Condition clone(){
		// TODO Auto-generated method stub
		return new Condition(this.literal,this.positive);
	}

	
	/**Reverse the positive flag
	 * 
	 */
	public void reverse()
	{
		this.positive=!this.positive;
	}
	
	
	public Condition()
	{
		
	}
	
	public Condition(String literal,boolean positive)
	{
		this.literal=literal;
		this.positive=positive;
	}

	public String getLiteral() {
		return literal;
	}

	public void setLiteral(String literal) {
		this.literal = literal;
	}

	public boolean isPositive() {
		return positive;
	}

	public void setPositive(boolean positive) {
		this.positive = positive;
	}
	
	/**Test whether have the same literal with another condition
	 * @param con the condition to compare with this condition
	 * @return if con has the same literal with this condition,will return true else false;
	 */
	public boolean hasSameLiteral(Condition con)
	{
		return this.literal.equals(con.getLiteral());
			
	}
	
	
	/**Create a condition that is positive with this one
	 * @return
	 */
	public Condition getNegativeCon()
	{
		Condition ne=new Condition();
		ne.setLiteral(this.getLiteral());
		ne.setPositive(!this.positive);
		return ne;
	}
	
	
	/**Test whether two condition is the same
	 * @param c
	 * @return
	 */
	public boolean isSame(Condition c)
	{
		return c.getLiteral().equals(this.getLiteral())
				&&	c.isPositive()==this.positive;
		
	}
	
	
	/**Test whether the condition is opposit with c
	 * @param c
	 * @return
	 */
	public boolean isOpposite(Condition c)
	{
		return c.getLiteral().equals(this.getLiteral())&&c.isPositive()!=this.positive;
	
	}
}
